﻿using DbUpgradeSqlite.General;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DbUpgradeSqlite
{
    public static class App
    {
        #region data数据库访问实例
        /// <summary>
        /// data数据库访问实例
        /// </summary>
        private static SqliteDal _dalData;
        /// <summary>
        /// data数据库访问实例
        /// </summary>
        public static SqliteDal DalData
        {
            get
            {
                if (_dalData == null)
                {
                    string path = Path.Combine(CodeHelper.CurrentPath, "dbdata.db");
                    _dalData = new SqliteDal(path);
                }
                return _dalData;
            }
        }
        #endregion

        /// <summary>
        /// 选择的数据库
        /// </summary>
        private static ChooseDbInfo _chooseDb = null;
        /// <summary>
        /// 选择的数据库
        /// </summary>
        public static ChooseDbInfo ChooseDb
        {
            get { return _chooseDb; }
            set
            {
                _chooseDb = value;
                if (_chooseDb != null)
                {
                    string dirBase = Path.Combine(CodeHelper.CurrentPath, "data");
                    string dir = Path.Combine(dirBase, _chooseDb.DbCheckName);
                    string dbPath = Path.Combine(dir, "dbupgrade.db");
                    if (!string.IsNullOrEmpty(dir))
                    {
                        DraftHelper.Instance.ReleaseFile("dbupgrade.db", dir);
                        if (Dal != null)
                        {
                            Dal.Dispose();
                            Dal = null;
                        }
                        Dal = new SqliteDal(dbPath);
                        Upgrade.Instance.UpDb();
                    }
                }
                else
                {
                    if (Dal != null)
                    {
                        Dal.Dispose();
                        Dal = null;
                    }
                }
            }
        }

        /// <summary>
        /// 脚本数据库访问实例
        /// </summary>
        public static SqliteDal Dal { get; private set; } = null;

        /// <summary>
        /// 是否选择脚本库
        /// </summary>
        /// <returns></returns>
        public static bool IsChooseDb
        {
            get
            {
                if (ChooseDb == null)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }

        /// <summary>
        /// 获取数据库路径
        /// </summary>
        /// <returns></returns>
        public static string GetDbPath()
        {
            if (App.IsChooseDb)
            {
                string dirBase = Path.Combine(CodeHelper.CurrentPath, "data");
                string dir = Path.Combine(dirBase, App.ChooseDb.DbCheckName);
                string dbPath = Path.Combine(dir, "dbupgrade.db");
                return dbPath;
            }
            else
            {
                return string.Empty;
            }
        }
    }
}
